package com.army.study.slice;

import com.army.study.ResourceTable;
import com.army.study.component.DeviceDialog;
import com.army.study.component.ListAdapter;
import com.army.study.databean.Item;
import com.army.study.utils.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.Context;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class RightAbilitySlice extends AbilitySlice implements IAbilityContinuation {

    private boolean isCirculation = false;  // 是否流转中
    private static String selectDeviceId;   // 选择的设备id
    private DeviceDialog deviceDialog;      // 设备选择

    private static Context context;         // 上下文
    private static int paramIndex;          // 页面跳转参数
    private static int removeIndex;         // 流转参数

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_right);
        context = getContext();

        // 如果页面跳转参数index为空,说明是流转过来参数, 使用流转removeIndex
        paramIndex = intent.getIntParam("index", removeIndex);
        Item item = Utils.getItem(paramIndex);

        // 初始化界面
        initView(item);
        // 初始化设备选择
        deviceDialog = new DeviceDialog(getContinuationRegisterManager(),RightAbilitySlice.this);
    }

    private void initView(Item item) {
        Image img = (Image)findComponentById(ResourceTable.Id_right_img);
        img.setPixelMap(item.getImg());

        Text title = (Text)findComponentById(ResourceTable.Id_right_title);
        title.setText(item.getTitle());

        Image btnSelect = (Image)findComponentById(ResourceTable.Id_imgConnect);
        Image btnShare = (Image)findComponentById(ResourceTable.Id_imgCirculation);

        btnSelect.setClickedListener(va -> {
            // 打开设备选择框（连接）
            deviceDialog.showDeviceList();
        });
        btnShare.setClickedListener(va -> {
            // 流转
            circulation();
        });
    }

    /**
     * 流转
     */
    private void circulation() {
        if (isCirculation) {
            Utils.createToastDialog(context, "正在流转，请稍后再试！");
            return;
        }
        if (selectDeviceId == null || "".equals(selectDeviceId)) {
            // 选择设备以后才能流转（迁移）
            Utils.createToastDialog(context, "请选择连接设备后进行流转操作！");
            return;
        }
        isCirculation = true;

        continueAbility(selectDeviceId);
    }

    /**
     * 设置选择设备Id
     * @param deviceId
     */
    public static void setDeviceId(String deviceId) {
        selectDeviceId = deviceId;
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        intentParams.setParam("removeIndex", paramIndex);
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        removeIndex = Integer.parseInt(intentParams.getParam("removeIndex").toString());
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}
